<?php

namespace app\admin\model;

use think\Model;


class Panda extends Model
{

    

    

    // 表名
    protected $name = 'panda';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_rent_text',
        'created_time_text',
        'is_compound_text'
    ];
    

    
    public function getIsRentList()
    {
        return ['1' => __('Is_rent 1'), '2' => __('Is_rent 2')];
    }

    public function getIsCompoundList()
    {
        return ['1' => __('Is_compound 1'), '2' => __('Is_compound 2')];
    }


    public function getIsRentTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_rent']) ? $data['is_rent'] : '');
        $list = $this->getIsRentList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreatedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['created_time']) ? $data['created_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsCompoundTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_compound']) ? $data['is_compound'] : '');
        $list = $this->getIsCompoundList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreatedTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function pandatype()
    {
        return $this->belongsTo('PandaType', 'type', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
